package com.example.gouwu2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class HomeActivity2 extends AppCompatActivity {

    // 声明控件
    private ImageView ivProduct;
    private TextView itemTitle, listMoney;
    private Button btnAddToCart;
    private ImageButton btnfanhui; // 返回按钮

    // 当前商品信息
    private String currentTitle;
    private String currentPrice;
    private int currentImageRes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home2);

        // 初始化控件
        ivProduct = findViewById(R.id.list_img);
        itemTitle = findViewById(R.id.item_title);
        listMoney = findViewById(R.id.list_money);
        btnAddToCart = findViewById(R.id.gouwuche);
        btnfanhui = findViewById(R.id.fanhui);

        // 获取HomeActivity传递的数据
        if (getIntent() != null) {
            currentTitle = getIntent().getStringExtra("title");
            currentPrice = getIntent().getStringExtra("price");
            currentImageRes = getIntent().getIntExtra("imageRes", R.drawable.d);

            // 更新界面显示
            itemTitle.setText(currentTitle);
            listMoney.setText(currentPrice);
            ivProduct.setImageResource(currentImageRes);
        }

        // 设置"加入购物车"按钮点击事件
        btnAddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建购物车项
                CartManager.CartItem cartItem = new CartManager.CartItem(
                        currentTitle,
                        currentPrice,
                        currentImageRes
                );

                // 添加到购物车管理器
                CartManager.getInstance().addItem(cartItem);

                // 显示成功提示
                Toast.makeText(HomeActivity2.this, "商品已添加到购物车", Toast.LENGTH_SHORT).show();
            }
        });

        // 设置返回按钮点击事件
        btnfanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 结束当前Activity，返回上一页
                finish();
            }
        });
    }
}